package com.gitee.qdbp.jdbc.test.service;

import java.util.Arrays;
import java.util.List;
import com.gitee.qdbp.jdbc.plugins.SqlDialect;
import com.gitee.qdbp.jdbc.sql.SqlBuffer;
import com.gitee.qdbp.jdbc.sql.SqlBuilder;
import com.gitee.qdbp.jdbc.sql.SqlTools;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 实体类工具
 *
 * @author zhaohuihua
 * @version 20201007
 */
public class EntityTools {

    private static String getCurrUserIdFromSession() {
        // 模拟从SESSION中获取当前登录用户ID
        return "U0000001";
    }

    private static List<String> getCurrDeptIdsFromSession() {
        // 模拟从SESSION中获取当前登录用户有权限的部门ID
        return Arrays.asList("D0000001", "D0000005", "D0000012");
    }

    /** 生成部门数据权限查询条件 **/
    public static SqlBuffer buildDeptDataPermission(String deptColumn, SqlDialect dialect) {
        return buildDeptDataPermission(null, null, deptColumn, dialect);
    }

    /** 生成部门数据权限查询条件 **/
    public static SqlBuffer buildDeptDataPermission(String userColumn, String deptColumn, SqlDialect dialect) {
        return buildDeptDataPermission(userColumn, null, deptColumn, dialect);
    }

    /** 生成部门数据权限查询条件 **/
    public static SqlBuffer buildDeptDataPermission(String userColumn, String agentColumn, String deptColumn,
            SqlDialect dialect) {
        if (VerifyTools.isAllBlank(userColumn, agentColumn, deptColumn)) {
            return new SqlBuffer();
        }
        SqlBuilder buffer = new SqlBuilder();
        if (VerifyTools.isNotBlank(userColumn)) {
            String userId = getCurrUserIdFromSession();
            buffer.adIf("OR").ad(userColumn).ad('=').var(userId);
        }
        if (VerifyTools.isNotBlank(agentColumn)) {
            String userId = getCurrUserIdFromSession();
            buffer.adIf("OR").ad(agentColumn).ad('=').var(userId);
        }
        if (VerifyTools.isNotBlank(deptColumn)) {
            List<String> deptIds = getCurrDeptIdsFromSession();
            buffer.adIf("OR").ad(SqlTools.buildInSql(deptColumn, deptIds, dialect));
        }
        return buffer.out();
    }
}
